﻿using GeuSoftWareBLL;

using NxStoreManage;

using System;
using System.Data.SqlClient;

namespace LengthAiCalculate
{
    [HideForm]
    public partial class SetDataConn : BaseForm
    {
        public int TabVisible = -1;

        public SetDataConn()
        {
            InitializeComponent();
            string db = Properties.Settings.Default.DbConn.Split(';')[0];
            if (db.Substring(db.LastIndexOf('=') + 1) != "")
            {
                textBox1.Text = db.Substring(db.LastIndexOf('=') + 1);
            }
            string sbdb = Properties.Settings.Default.SbDbConn.Split(';')[0];
            if (sbdb.Substring(sbdb.LastIndexOf('=') + 1) != "")
            {
                textBox8.Text = sbdb.Substring(sbdb.LastIndexOf('=') + 1);
            }
            if (TabVisible == 0)
            {
                tabPage1.Text = "锯切业务数据库(待配置)";
            }
            if (TabVisible == 1)
            {
                tabPage2.Text = "换号系统数据库(待配置)";
            }
            if (TabVisible == 2)
            {
                tabPage1.Text = "锯切业务数据库(待配置)";
                tabPage2.Text = "换号系统数据库(待配置)";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" || textBox4.Text.Trim() == "")
            {
                ShowMsg("数据库地址、数据库名、用户名、密码不能为空");
                return;
            }
            string dbconn = $@"Data Source={textBox1.Text.Trim()};Initial Catalog={textBox4.Text.Trim()};User ID={textBox2.Text.Trim()};Password={textBox3.Text.Trim()}";
            using (SqlConnection conn = new SqlConnection(dbconn))
            {
                try
                {
                    conn.Open();
                    Properties.Settings.Default.DbConn = DPAPI.Encrypt(DPAPI.KeyType.MachineKey, dbconn, "GeuSoftWare.com", "南京好易信息技术有限公司");
                    Properties.Settings.Default.Save();
                    if (string.IsNullOrEmpty(Properties.Settings.Default.SbDbConn))
                    {
                        ShowMsg("锯切系统数据库保存成功,请设置换号系统数据库！");
                    }
                    else
                    {
                        string sbconn = DPAPI.Decrypt(Properties.Settings.Default.SbDbConn, "GeuSoftWare.com", out _);
                        using (SqlConnection sbconnn = new SqlConnection(sbconn))
                        {
                            try
                            {
                                sbconnn.Open();
                                ShowMsg("数据库保存成功,程序自动关闭，请重新打开!");
                                this.Close();
                                this.Dispose();
                            }
                            catch (Exception ex)
                            {
                                ShowMsg("换号系统数据库连接失败,请重新设置!" + ex.Message);
                                return;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    ShowMsg("锯切系统数据库连接失败!" + ex.Message);
                }
            }
        }

        private void SetDataConn_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox8.Text.Trim() == "" || textBox5.Text.Trim() == "" || textBox7.Text.Trim() == "" || textBox6.Text.Trim() == "")
            {
                ShowMsg("数据库地址、数据库名、用户名、密码不能为空");
                return;
            }
            string dbconn = $@"Data Source={textBox8.Text.Trim()};Initial Catalog={textBox5.Text.Trim()};User ID={textBox7.Text.Trim()};Password={textBox6.Text.Trim()}";
            using (SqlConnection conn = new SqlConnection(dbconn))
            {
                try
                {
                    conn.Open();
                    Properties.Settings.Default.SbDbConn = DPAPI.Encrypt(DPAPI.KeyType.MachineKey, dbconn, "GeuSoftWare.com", "南京好易信息技术有限公司");
                    Properties.Settings.Default.Save();

                    if (string.IsNullOrEmpty(Properties.Settings.Default.DbConn))
                    {
                        ShowMsg("换号系统数据库保存成功,请设置锯切系统数据库！");
                    }
                    else
                    {
                        string sbconn = DPAPI.Decrypt(Properties.Settings.Default.DbConn, "GeuSoftWare.com", out _);
                        using (SqlConnection sbconnn = new SqlConnection(sbconn))
                        {
                            try
                            {
                                sbconnn.Open();
                                ShowMsg("数据库保存成功,程序自动关闭，请重新打开!");
                                this.Close();
                                this.Dispose();
                            }
                            catch (Exception ex)
                            {
                                ShowMsg("锯切系统数据库连接失败,请重新设置!" + ex.Message);
                                return;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    ShowMsg("数据库连接失败!" + ex.Message);
                }
            }
        }
    }
}